import type { ApiResponse, CartItem } from '../types/index';

export const cartApi = {
  async getCart(): Promise<ApiResponse<{ items: CartItem[] }>> {
    const response = await fetch('/api/cart');
    return response.json();
  },

  async addToCart(courseId: number): Promise<ApiResponse<CartItem>> {
    const response = await fetch('/api/cart', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ courseId }),
    });
    return response.json();
  },

  async removeFromCart(courseId: number): Promise<ApiResponse<void>> {
    const response = await fetch(`/api/cart/${courseId}`, {
      method: 'DELETE',
    });
    return response.json();
  },

  async updateQuantity(courseId: number, quantity: number): Promise<ApiResponse<CartItem>> {
    const response = await fetch(`/api/cart/${courseId}`, {
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ quantity }),
    });
    return response.json();
  },

  async clearCart(): Promise<ApiResponse<void>> {
    const response = await fetch('/api/cart', {
      method: 'DELETE',
    });
    return response.json();
  }
};